<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>我的订单</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        .order-card {
            transition: transform 0.2s;
        }
        .order-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .product-image {
            width: 80px;
            height: 80px;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <div layout:fragment="content">
        <div class="row mb-4">
            <div class="col">
                <h2>我的订单</h2>
            </div>
        </div>

        <!-- 筛选栏 -->
        <div class="card mb-4">
            <div class="card-body">
                <form th:action="@{/orders}" method="get" class="row g-3">
                    <div class="col-md-3">
                        <input type="text" class="form-control" name="keyword" 
                               th:value="${param.keyword}" placeholder="搜索订单号或商品名称">
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="status">
                            <option value="">全部状态</option>
                            <option value="0" th:selected="${param.status == '0'}">待发货</option>
                            <option value="1" th:selected="${param.status == '1'}">已发货</option>
                            <option value="2" th:selected="${param.status == '2'}">已完成</option>
                            <option value="3" th:selected="${param.status == '3'}">已取消</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="timeRange">
                            <option value="">全部时间</option>
                            <option value="7" th:selected="${param.timeRange == '7'}">最近7天</option>
                            <option value="30" th:selected="${param.timeRange == '30'}">最近30天</option>
                            <option value="90" th:selected="${param.timeRange == '90'}">最近90天</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-2"></i>搜索
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 订单列表 -->
        <div th:if="${#lists.isEmpty(orders)}" class="text-center py-5">
            <i class="fas fa-file-alt fa-3x text-muted mb-3"></i>
            <h5>暂无订单</h5>
            <a th:href="@{/products}" class="btn btn-primary mt-3">
                去购物
            </a>
        </div>

        <div th:if="${not #lists.isEmpty(orders)}" class="row g-4">
            <div class="col-12" th:each="order : ${orders}">
                <div class="card order-card">
                    <div class="card-header bg-transparent">
                        <div class="row align-items-center">
                            <div class="col">
                                <span class="text-muted me-3" th:text="${order.orderNo}">订单号</span>
                                <small class="text-muted" 
                                       th:text="${#temporals.format(order.createTime, 'yyyy-MM-dd HH:mm')}">
                                    下单时间
                                </small>
                            </div>
                            <div class="col-auto">
                                <span th:class="'badge ' + 
                                    ${order.status == 0 ? 'bg-warning' : 
                                    (order.status == 1 ? 'bg-info' : 
                                    (order.status == 2 ? 'bg-success' : 'bg-secondary'))}"
                                    th:text="${order.status == 0 ? '待发货' : 
                                            (order.status == 1 ? '已发货' : 
                                            (order.status == 2 ? '已完成' : '已取消'))}">
                                    状态
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <div class="d-flex align-items-center mb-3" 
                                     th:each="item : ${order.items}">
                                    <img th:src="${item.productImage ?: '/static/images/default-product.jpg'}"
                                         class="product-image me-3" alt="商品图片">
                                    <div>
                                        <h6 class="mb-1" th:text="${item.productName}">商品名称</h6>
                                        <small class="text-muted" th:text="${item.specifications}">
                                            商品规格
                                        </small>
                                        <div>
                                            <span class="text-danger me-2" 
                                                  th:text="${'¥' + #numbers.formatDecimal(item.price, 1, 2)}">
                                                ¥0.00
                                            </span>
                                            <span class="text-muted" th:text="${'x' + item.quantity}">x1</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4 text-md-end">
                                <div class="mb-3">
                                    <span class="text-muted me-2">总计:</span>
                                    <span class="text-danger fw-bold h5 mb-0" 
                                          th:text="${'¥' + #numbers.formatDecimal(order.totalAmount, 1, 2)}">
                                        ¥0.00
                                    </span>
                                </div>
                                <div>
                                    <a th:href="@{/orders/{id}(id=${order.id})}" 
                                       class="btn btn-outline-primary btn-sm me-2">
                                        订单详情
                                    </a>
                                    <button th:if="${order.status == 0}" 
                                            class="btn btn-outline-danger btn-sm"
                                            th:onclick="'cancelOrder(' + ${order.id} + ')'">
                                        取消订单
                                    </button>
                                    <button th:if="${order.status == 1}" 
                                            class="btn btn-success btn-sm"
                                            th:onclick="'confirmReceipt(' + ${order.id} + ')'">
                                        确认收货
                                    </button>
                                    <button th:if="${order.status == 2}" 
                                            class="btn btn-primary btn-sm"
                                            th:onclick="'window.location.href=\'/orders/' + ${order.id} + '/review\''">
                                        评价
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 分页 -->
        <nav th:if="${totalPages > 1}" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item" th:classappend="${currentPage == 1} ? 'disabled'">
                    <a class="page-link" th:href="@{/orders(page=${currentPage - 1}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}">
                        上一页
                    </a>
                </li>
                <li class="page-item" th:each="pageNum : ${#numbers.sequence(1, totalPages)}"
                    th:classappend="${pageNum == currentPage} ? 'active'">
                    <a class="page-link" th:href="@{/orders(page=${pageNum}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}"
                       th:text="${pageNum}">1</a>
                </li>
                <li class="page-item" th:classappend="${currentPage == totalPages} ? 'disabled'">
                    <a class="page-link" th:href="@{/orders(page=${currentPage + 1}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}">
                        下一页
                    </a>
                </li>
            </ul>
        </nav>
    </div>

    <script th:inline="javascript">
        function cancelOrder(orderId) {
            if (confirm('确定要取消这个订单吗？')) {
                fetch(`/orders/${orderId}/cancel`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }

        function confirmReceipt(orderId) {
            if (confirm('确认已收到商品吗？')) {
                fetch(`/orders/${orderId}/confirm`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }
    </script>
</body>
</html> 